package cn.eng.basic.service.impl;

import cn.eng.basic.commonenum.ErrorCode;
import cn.eng.basic.domain.dto.PhoneRegisterDto;
import cn.eng.basic.exception.BusinessException;
import cn.eng.basic.service.IVerifyCodeService;
import cn.eng.basic.utils.StrUtils;
import cn.eng.basic.utils.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取图形验证码
     * @param imageCodekey
     * @return
     */
    @Override
    public String getVerifyCode(String imageCodekey) {
        //1.获取随机图形验证码
        String code = StrUtils.getComplexRandomString(4);
        //2.将图形码写到图片中并且使用base64编码 并返回base64编码的字符串
        String base64 = VerifyCodeUtils.verifyCode(115, 40, code);
        //3. 将图形验证码保存在redis中
        redisTemplate.opsForValue().set(imageCodekey,code,5, TimeUnit.MINUTES);
        //返回图形验证码
        return base64;
    }

    /**
     * 注册时 - 获取手机验证号码
     * @param phoneRegisterDto
     */
    @Override
    public void getSmsCode( PhoneRegisterDto phoneRegisterDto) {
        String phone = phoneRegisterDto.getPhone();//获取手机号
        String imageCode = phoneRegisterDto.getImageCode();//获取图形验证码
        String imageCodekey = phoneRegisterDto.getImageCodekey();//获取图形验证码的key
        Object  imageCodeValue= redisTemplate.opsForValue().get(imageCodekey);
        //判断redis里面图形验证码是否为null  如为null 表示图形验证码已过期
        if (imageCodeValue == null){
            throw new BusinessException(ErrorCode.IMGCODE_TIME_OUT.getCode());
        }
        //判断redis里面的验证码和用户输入的验证码是否一致
        if (!imageCodeValue.equals(imageCode)){
            throw new BusinessException(ErrorCode.IMGCODE_ERROR.getCode());
        }
        Object phoneCode = redisTemplate.opsForValue().get("register:" + phone);
        String code=null;
        if (phoneCode ==null){
             code = StrUtils.getRandomString(4);
        }else {
            long lodTime =Long.parseLong(phoneCode.toString().split(":")[1]);
            System.out.println("============="+lodTime);
            long newTime = System.currentTimeMillis();
            if ((newTime-lodTime)<=1*60*1000){
                throw new BusinessException(ErrorCode.PHONECODE_ERROR.getCode());
            }
             code = phoneCode.toString().split(":")[0];
            System.out.println("-------------"+code);
        }
        redisTemplate.opsForValue().set("register:" + phone,code+":"+System.currentTimeMillis(),5,TimeUnit.MINUTES);
        //SmsUtils.sendSms("13698296250","你的验证码为："+code+"请在五分钟之内使用");
        System.out.println("你的验证码为："+code+"请在五分钟之内使用");
    }

    /**
     * 微信登录绑定  - 获取手机验证码
     * @param map
     */
    @Override
    public void getBinderSmsCode(Map<String, String> map) {
        //获取手机号
        String phone = map.get("phone");
        //判断手机号是否为空
        if (phone == null){
            throw new BusinessException(ErrorCode.PHONE_NULL.getCode());
        }
        Object phoneCode = redisTemplate.opsForValue().get("binder:" + phone);
        String code=null;
        if (phoneCode ==null){
            code = StrUtils.getRandomString(4);
        }else {
            long lodTime =Long.parseLong(phoneCode.toString().split(":")[1]);
            long newTime = System.currentTimeMillis();
            if ((newTime-lodTime)<=1*60*1000){
                throw new BusinessException(ErrorCode.PHONECODE_ERROR.getCode());
            }
            code = phoneCode.toString().split(":")[0];
        }
        redisTemplate.opsForValue().set("binder:" + phone,code+":"+System.currentTimeMillis(),5,TimeUnit.MINUTES);
        //SmsUtils.sendSms("13698296250","你的验证码为："+code+"请在五分钟之内使用");
        System.out.println("你的验证码为："+code+"请在五分钟之内使用");
    }
}
